//
//  YBForgetPwdVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/7.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON
import EachNavigationBar

class YBForgetPwdVC: BaseVC {
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigation.bar.alpha = 0
        view.backgroundColor = .lightWhiteDark27
        view.addSubview(forgetPwdView)
        
        forgetPwdView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.bk_setBackBarButtonItem(isHidden: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.bk_setPopSwipe(false)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        forgetPwdView.endEditing(true)
    }
    
    // MARK: - lazy
    lazy var vm: YBLoginViewModel = {
        let vm = YBLoginViewModel()
        return vm
    }()
    
    private lazy var forgetPwdView: YBForgetPwdView = {
        let view = YBForgetPwdView()
        view.delegate = self
        return view
    }()
    
}

// MARK: - YBLoginViewDelegate
extension YBForgetPwdVC: YBForgetPwdViewDelegate {
    
    func forgetPwdViewGetCode() {
        forgetPwdView.endEditing(true)
        self.get_forgetPwdCode()
    }
    
    func forgetPwdViewDidClick(_ btnType: YBForgetPwdView.BtnType) {
        switch btnType {
        case .confirm:
            forgetPwdView.endEditing(true)
            self.post_forgetPwd()
        case .login:
            self.navigationController?.popViewController(animated: true)
        case .register:
            let vc = YBRegisterVC()
            self.navigationController?.pushViewController(vc)
        }
    }
    
    func forgetPwdViewInput(_ inputType: YBForgetPwdView.InputType, content: String) {
        PPP("\(inputType.style.des): \(content)")
        switch inputType {
        case .phone:
            vm.registerModel.mobile = content.removeAllSpace
        case .code:
            vm.registerModel.code = content
        case .pwd:
            vm.registerModel.password = content.removeAllSpace
        case .newPwd:
            vm.registerModel.newPassword = content.removeAllSpace
        }
    }
    
}

// MARK: - 网络请求
extension YBForgetPwdVC {
    
    fileprivate func get_forgetPwdCode() {
        guard vm.registerModel.mobile.isValidPhone() else {
            HintTool.toast("请输入正确手机号")
            return
        }
        vm.req_forgetPwdCode(mobile: vm.registerModel.mobile) { [weak self] ok, obj, msg, code in
            if ok {
                self?.forgetPwdView.gcdWithGetCode(need: true)
                HintTool.toast("验证码已发送到你的手机,请注意查收!")
            } else {
                HintTool.toast(msg)
            }
        }
    }
    
    fileprivate func post_forgetPwd() {
        guard forgetPwdView.checkBtn.isSelected else {
            HintTool.toast("请勾选协议")
            return
        }
        guard let params = vm.validRegisterParams(type: .forgetPwd, { msg in
            HintTool.toast(msg)
        }) else { return }
        
        vm.req_forgetPwd(params) { [weak self] ok, obj, msg, code in
            if ok {
                HintTool.toast("设置成功")
                TaskTool.delay(0.5) {
                    self?.navigationController?.popViewController(animated: true)
                }
            } else {
                HintTool.toast(msg)
            }
        }
    }
    
}
